package cn.com.pajx.pajx_visitor_v3.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.com.pajx.pajx_visitor_v3.R;

/**
 * create by ruancw on 2024/2/28
 * description 时间选择
 */
public class TimePickerUtil {

    private boolean[] TIME_TYPE = new boolean[]{true, true, true, true, true, false};
    public static boolean[] ALL_MINUTE = new boolean[]{true, true, true, true, true, false};//年月日时分
    public static boolean[] YEAR_MONTH_DAY = new boolean[]{true, true, true, false, false, false};//年月日
    public static boolean[] YEAR_MONTH = new boolean[]{true, true, false, false, false, false};////年月
    public static boolean[] HOUR_MINUTE_SECOND = new boolean[]{false, false, false, true, true, true};//时分秒
    public static boolean[] HOUR_MINUTE = new boolean[]{false, false, false, true, true, false};//时分
    private Context context;
    private TextView tvStartTime, tvEndTime, tvTime;
    private String start_time, end_time, time, dateFormat;
    private boolean isShow;
    private TimeSelectListener timeSelectListener;

    public TimePickerUtil(Context context, TextView tvTime, String dateFormat) {
        this.context = context;
        this.tvTime = tvTime;
        this.dateFormat = dateFormat;
    }

    /**
     *
     * @param context context
     * @param isShow 是否显示刷新数据提示
     * @param tvStartTime 显示开始时间的控件
     * @param tvEndTime 显示结束时间的控件
     * @param dateFormat 时间的显示格式
     */
    public TimePickerUtil(Context context, boolean isShow, TextView tvStartTime, TextView tvEndTime, String dateFormat) {
        this.context = context;
        this.tvStartTime = tvStartTime;
        this.tvEndTime = tvEndTime;
        this.isShow = isShow;
        this.dateFormat = dateFormat;
    }

    public TimePickerUtil(Context context, boolean isShow, TextView tvStartTime, TextView tvEndTime, TextView tvTime, String dateFormat) {
        this.context = context;
        this.tvStartTime = tvStartTime;
        this.tvEndTime = tvEndTime;
        this.isShow = isShow;
        this.tvTime = tvTime;
        this.dateFormat = dateFormat;
    }

    /**
     * 初始化开始时间选择器
     *
     * @param isLessThan true 小于 false 大于
     * @param ignoreTips 是否忽略提示 针对OA的模块
     *                   OA的相关模块需要忽略为true
     */
    public void initStartTimePicker(final boolean isLessThan, final boolean ignoreTips) {

        @SuppressLint("SimpleDateFormat") TimePickerView tpvStart = new TimePickerBuilder(context, (date, v) -> {
            if (!ignoreTips) {
                boolean isShowTip = showTips(date, isLessThan, "开始时间不能小于当前时间", "开始时间不能大于当前时间");
                if (isShowTip) return;
            }

            start_time = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
            tvStartTime.setText(new SimpleDateFormat(dateFormat).format(date));
            if (isShow) {
                tvEndTime.setText("");
                ToastUtil.toast("选择结束时间后可刷新数据");
            }

            timeSelectListener.startTimeSelected(start_time);
        })
                .setType(TIME_TYPE)
                .setTitleText("选择时间")
                .setDate(Calendar.getInstance())
                .setOutSideCancelable(true)
                .setLineSpacingMultiplier(2.0f)
                .isCyclic(false)
                .setCancelColor(context.getResources().getColor(R.color.colorPrimary))
                .setSubmitColor(context.getResources().getColor(R.color.colorPrimary))
                .build();
        tpvStart.show();
    }

    /**
     * 初始化结束时间选择器
     * ignoreConstraint : 忽略掉 判断选择开始时间
     */
    public void initEndTimePicker(final String startTime, final boolean isLessThan, final boolean ignoreTips) {
        @SuppressLint({"SimpleDateFormat", "SetTextI18n"}) TimePickerView tpvEnd = new TimePickerBuilder(context, (date, v) -> {
            if (!ignoreTips) {
                boolean isShowTip = showTips(date, isLessThan, "结束时间不能小于当前时间", "结束时间不能大于当前时间");
                if (isShowTip) return;
            }

            end_time = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
            if (DateUtil.date2TimeStamp(startTime.substring(0, 12), "yyyyMMddHHmm") >=
                    DateUtil.date2TimeStamp(end_time.substring(0, 12), "yyyyMMddHHmm")) {
                ToastUtil.toast("结束时间不能小于开始时间");
                return;
            }
            if (isShow) {
                tvEndTime.setText("--" + new SimpleDateFormat(dateFormat).format(date));
            } else {
                tvEndTime.setText(new SimpleDateFormat(dateFormat).format(date));
            }
            timeSelectListener.endTimeSelected(end_time);
        })
                .setType(TIME_TYPE)
                .setTitleText("选择时间")
                .setDate(Calendar.getInstance())
                .setOutSideCancelable(true)
                .setLineSpacingMultiplier(2.0f)
                .isCyclic(false)
                .setCancelColor(context.getResources().getColor(R.color.colorPrimary))
                .setSubmitColor(context.getResources().getColor(R.color.colorPrimary))
                .build();

        tpvEnd.show();
    }

    private boolean showTips(Date date, boolean isLessThan, String lessToast, String greaterToast) {
        if (isLessThan) {
            if (date.getTime() / (1000 * 60) < System.currentTimeMillis() / (1000 * 60)) {
                ToastUtil.toast(lessToast);
                return true;
            }
        } else {
            if (date.getTime() / (1000 * 60) > System.currentTimeMillis() / (1000 * 60)) {
                ToastUtil.toast(greaterToast);
                return true;
            }
        }
        return false;
    }

    /**
     * 初始化时间选择器
     */
    public void initTimePicker(final String des, final boolean isLessThan,boolean ignoreTips) {
        @SuppressLint("SimpleDateFormat") TimePickerView tpvTime = new TimePickerBuilder(context, (date, v) -> {
            if (!ignoreTips){
                if (showTips(date, isLessThan, des + "时间不能小于当前时间", des + "时间不能大于当前时间")) return;
            }


            time = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
            tvTime.setText(new SimpleDateFormat(dateFormat).format(date));
            timeSelectListener.timeSelected(time);
        })
                .setType(TIME_TYPE)
                .setTitleText("选择时间")
                .setDate(Calendar.getInstance())
                .setOutSideCancelable(true)
                .setLineSpacingMultiplier(2.5f)
                .isCyclic(false)
                .setCancelColor(context.getResources().getColor(R.color.colorPrimary))
                .setSubmitColor(context.getResources().getColor(R.color.colorPrimary))
                .build();

        tpvTime.show();
    }

    /**
     * 设置选择器的格式
     *
     * @param TIME_TYPE 时间格式
     */
    public void setTimeType(boolean[] TIME_TYPE) {
        this.TIME_TYPE = TIME_TYPE;
    }

    /**
     * 接口绑定
     *
     * @param timeSelectListener 接口
     */
    public void setTimeSelectListener(TimeSelectListener timeSelectListener) {
        this.timeSelectListener = timeSelectListener;
    }

    /**
     * 监听时间选择的接口
     */
    public interface TimeSelectListener {
        //设置回调的方法
        void startTimeSelected(String startTime);

        void endTimeSelected(String endTime);

        void timeSelected(String time);
    }

}
